package com.gk.carshopsys.controller;

import com.alibaba.excel.EasyExcel;
import com.gk.carshopsys.annotation.Log;
import com.gk.carshopsys.pojo.Car;
import com.gk.carshopsys.pojo.Cust;
import com.gk.carshopsys.service.CarService;
import com.gk.carshopsys.utils.CarDataListener;
import com.gk.carshopsys.utils.CustDataListener;
import com.gk.carshopsys.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CarController {
    @Autowired
    private CarService carService;


    //Excel参数
    private HashMap<String,String> param;


    //excel传参
    @PostMapping("/carlist/excelparamset")
    @ResponseBody
    public void excelset(@RequestBody HashMap<String,String> params){
        param= params;
    }

    //excel下载
    @Log("导出汽车库存")
    @GetMapping("/carlist/excelexport")
    @ResponseBody
    public String excelexport(HttpServletResponse response) throws Exception {
        //导出的参数
        String id_s = param.get("id");
        String carname = param.get("carname");
        String brand = param.get("brand");
        String color = param.get("color");
        String type = param.get("type");
        String configuration = param.get("configuration");
        String low_price = param.get("low_price");//需要转型
        String hight_price = param.get("hight_price");//需要转型

        Car car = new Car();

        if(id_s!=null && !id_s.equals("")){
            try {
                Long id = Long.valueOf(id_s);
                car.setId(id);
            }catch (ClassCastException e){
            }finally {
                car.setBrand(brand);
                car.setCarname(carname);
                car.setColor(color);
                car.setType(type);
                car.setConfiguration(configuration);
            }

        }
        car.setBrand(brand);
        car.setCarname(carname);
        car.setColor(color);
        car.setType(type);
        car.setConfiguration(configuration);


        ExcelUtils.export2Web(response, "汽车库存表", "汽车库存信息",Car.class,carService.findCar(car,low_price,hight_price));
        return "success";
        }


    // easyexcel上传文件
    @Log("导入汽车库存")
    @PostMapping("/carlist/excelimport")
    public String upload(MultipartFile file, RedirectAttributesModelMap model) throws IOException {
        EasyExcel.read(file.getInputStream(), Car.class, new CarDataListener(carService)).sheet().doRead();
        return "redirect:/carlist";
    }



    //批量删除车型
    @Log("删除汽车库存")
    @ResponseBody
    @RequestMapping("/carlist/del")
    public Map<String,Integer> carlistdel(@RequestBody List<Integer> ids){
        int result = carService.delCar(ids);
        Map<String,Integer> map = new HashMap<>();
        map.put("code",result);
        return map;
    }

    @Log("更新汽车库存")
    @RequestMapping("/carlist/update")
    public String carUpdate(@RequestParam("update_id") Long id,
                            @RequestParam("update_brand") String brand,
                            @RequestParam("update_carname") String carname,
                            @RequestParam("update_price") Long price,
                            @RequestParam("update_color") String color,
                            @RequestParam("update_type") String type,
                            @RequestParam("update_quantity") Long quantity,
                            @RequestParam("update_configuration") String configuration,
                            RedirectAttributesModelMap model){

        Car car = new Car(id,brand,carname,price,color,type,quantity,configuration);
        int result =carService.updateCar(car);

        if(result==1){
            model.addFlashAttribute("addmsg","修改成功");
        }else {
            model.addFlashAttribute("addmsg","修改失败");
        }

        return "redirect:/carlist";
    }



    //查询车辆库存
    @Log("查询汽车库存")
    @RequestMapping("/carlist/query")
    @ResponseBody
    public List<Car> queryCar(@RequestBody HashMap<String,String> params){
        String id_s = params.get("id");
        String carname = params.get("carname");
        String brand = params.get("brand");
        String color = params.get("color");
        String type = params.get("type");
        String configuration = params.get("configuration");
        String low_price = params.get("low_price");//需要转型
        String hight_price = params.get("hight_price");//需要转型




        Car car = new Car();

        if(id_s!=null && !id_s.equals("")){
            try {
                Long id = Long.valueOf(id_s);
                car.setId(id);
            }catch (ClassCastException e){
            }finally {
                car.setBrand(brand);
                car.setCarname(carname);
                car.setColor(color);
                car.setType(type);
                car.setConfiguration(configuration);
            }

        }
        car.setBrand(brand);
        car.setCarname(carname);
        car.setColor(color);
        car.setType(type);
        car.setConfiguration(configuration);



        return carService.findCar(car,low_price,hight_price);
    }
    @Log("添加汽车库存")
    @RequestMapping("/carlist/add")
    public String carAdd(@RequestParam("add_brand") String brand,
                         @RequestParam("add_carname") String carname,
                         @RequestParam("add_price") Long price,
                         @RequestParam("add_color") String color,
                         @RequestParam("add_type") String type,
                         @RequestParam("add_quantity") Long quantity,
                         @RequestParam("add_configuration") String configuration,
                         RedirectAttributesModelMap model){

        Car car = new Car(brand,carname,price,color,type,quantity,configuration);
        int result =carService.addCar(car);

        if(result==1){
            model.addFlashAttribute("addmsg","添加车型成功");
        }else {
            model.addFlashAttribute("addmsg","添加车型失败,未知错误");
        }

        return "redirect:/carlist";
    }



    //跳转车辆库存管理页面
    @RequestMapping("/carlist")
    public String tocarlist(){
        return "carlist";
    }
}
